package com.cssw.fyzb.busi.api.user;


import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.core.dao.user.DistrictDao;
import com.cssw.fyzb.core.model.dodb.District;
import com.cssw.fyzb.core.model.dodb.User;
import com.cssw.fyzb.core.model.vo.user.UserInfoVo;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

/**
 * 获取用户信息
 *
 * @author YangYiQun
 */
@Function(description = "获取用户信息", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true)
})
@Component
public class GetUserInfo implements Action {

    @Autowired
    private UserService userService;

    @Autowired
    private DistrictDao districtDao;

    private static final Logger logger = LoggerFactory.getLogger(GetUserInfo.class);

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        try {
            User user = userService.getById(userId);
            UserInfoVo userInfo = new UserInfoVo();
            userInfo.setUserId(user.getUserId());
            userInfo.setSupUserId(user.getSupUserId());
            userInfo.setUserName(user.getUserName());
            userInfo.setNickName(user.getNickName());
            userInfo.setMobile(user.getMobile());
            userInfo.setProvince(user.getProvince());
            userInfo.setCity(user.getCity());
            userInfo.setDistrict(user.getDistrict());

            // 地区名称
            if (user.getDistrict() != null) {
                District district = districtDao.getByCode(user.getDistrict());
                if (district != null) {
                    userInfo.setDistrict(district.getDistrictName());
                }
            }
            // 城市名称
            if (user.getCity() != null) {
                District district = districtDao.getByCode(user.getCity());
                if (district != null) {
                    userInfo.setCity(district.getDistrictName());
                }
            }
            // 省
            if (user.getProvince() != null) {
                District district = districtDao.getByCode(user.getProvince());
                if (district != null) {
                    userInfo.setProvince(district.getDistrictName());
                }
            }

            userInfo.setStatus(user.getStatus());
            userInfo.setSex(user.getSex());
//            userInfo.setIdCard(user.getIdCard());
            if (user.getBirthday() != null) {
                SimpleDateFormat formatterBirthday = new SimpleDateFormat("yyyy-MM-dd");
                userInfo.setBirthday(formatterBirthday.format(user.getBirthday()));
            }
            userInfo.setImsi(user.getImsi());
            userInfo.setFaceAuth(user.getFaceAuth());
            userInfo.setUserLevel(user.getUserLevel());
            userInfo.setTeamLevel(user.getTeamLevel());
            if (user.getCreateTime() != null) {
                SimpleDateFormat formatterTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                userInfo.setCreateTime(formatterTime.format(user.getCreateTime()));
            }
            userInfo.setSign(user.getSign());
            userInfo.setAvatar(user.getAvatar());

            userInfo.setAuthPay(user.getAuthPay());
            userInfo.setHrn(getHrn(user.getAuthPay(), user.getFace(), user.getFaceAuth()));
            userInfo.setHrnName(getHrnName(user.getAuthPay(), user.getFace(), user.getFaceAuth()));
            userInfo.setFace(user.getFace());
            userInfo.setFyNo(user.getFyNo());
            userInfo.setFyNoNum(user.getFyNoNum());
            userInfo.setPendantUrl(user.getPendantUrl());
            userInfo.setVip(user.getVip());
            if (user.getVipStartTime() != null) {
                SimpleDateFormat formatterTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                userInfo.setVipStartTime(formatterTime.format(user.getVipStartTime()));
            }
            if (user.getVipEndTime() != null) {
                SimpleDateFormat formatterTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                userInfo.setVipEndTime(formatterTime.format(user.getVipEndTime()));
            }

            return Response.success().put("data", userInfo);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("用户信息获取失败");
        }
    }

    // 1支付页面 2人脸认证页面 3不进页面
    private Integer getHrn(String authPay, String face, String faceAuth) {
        if (User.FACE_AUTH_YES.equals(faceAuth)){
            return 3;
        }
        authPay = authPay == null ? "0" : authPay;
        face = face == null ? "0" : face;
        //hrn==1   跳支付     2，跳实名认证  3， 跳展示实名认证信息
        switch (authPay + face) {
            case "00":
                return 2;
            case "01":
                return 1;// 人脸认证通过，不进页面
            case "10":
                return 2;
            case "11":
                return 3;
        }
        return null;
    }

    // 1支付页面 2人脸认证页面 3不进页面
    private String getHrnName(String authPay, String face, String faceAuth) {
        if (User.FACE_AUTH_YES.equals(faceAuth)){
            return "已实名";
        }
        authPay = authPay == null ? "0" : authPay;
        face = face == null ? "0" : face;
        //hrn==1   跳支付     2，跳实名认证  3， 跳展示实名认证信息
        switch (authPay + face) {
            case "00":
                return "未实名";
            case "01":
                return "已认证未支付";
            case "10":
                return "已支付未认证";
            case "11":
                return "已实名";
        }
        return "未实名";
    }

}

